package com.yiren.manager.service.impl.elder;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.exception.MyException;
import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.Favorites;
import com.yiren.entity.YrBeHospitalPledge;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrElderFamilyMember;
import com.yiren.entity.YrElderMemContact;
import com.yiren.entity.YrElderOutOrder;
import com.yiren.entity.YrElderWallet;
import com.yiren.entity.YrMealCard;
import com.yiren.entity.YrOutFeeRecord;
import com.yiren.entity.YrPaper;
import com.yiren.entity.YrPaperRecord;
import com.yiren.entity.YrRoomAssetComp;
import com.yiren.manager.repository.assets.YrRoomAssetCompRepository;
import com.yiren.manager.repository.cost.YrOutFeeRecordRepository;
import com.yiren.manager.repository.cost.YrPaperRecordRepository;
import com.yiren.manager.repository.cost.YrPaperRepository;
import com.yiren.manager.repository.elder.YrElderBeHospitalRepository;
import com.yiren.manager.repository.elder.YrElderFamilyMemberRepository;
import com.yiren.manager.repository.elder.YrElderMemContactRepository;
import com.yiren.manager.repository.elder.YrElderOutOrderRepository;
import com.yiren.manager.repository.elder.YrElderRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.repository.lease.YrBeHospitalPledgeRepository;
import com.yiren.manager.repository.messhall.YrMealCardRepositoty;
import com.yiren.manager.service.iface.elder.YrElderService;

@Service("yrElderService")
public class YrElderServiceImpl extends GenericServiceImpl<YrElder, String> implements YrElderService{

	@Autowired
	private YrElderFamilyMemberRepository familyMemberRepository;
	
	@Autowired
	private YrElderMemContactRepository memContactRepository;
	
	@Autowired
	private YrBeHospitalPledgeRepository beHospitalPledgeRepository;
	
	@Autowired
	private YrElderWalletRepository elderWalletRepository;
	
	@Autowired
	private YrMealCardRepositoty mealCardRepositoty;
	
	@Autowired
	private YrRoomAssetCompRepository roomAssetCompRepository;
	
	@Autowired
	private YrElderBeHospitalRepository elderBeHospitalRepository;
	
	@Autowired
	private YrElderOutOrderRepository elderOutOrderRepository;
	
	@Autowired
	private YrOutFeeRecordRepository outFeeRecordRepository;
	
	@Autowired
	private YrPaperRecordRepository paperRecordRepository;
	
	@Autowired
	private YrPaperRepository paperRepository;
 	
	@Autowired
	public YrElderServiceImpl(YrElderRepository repository) {
		super(repository);
		// TODO Auto-generated constructor stub
	}

	@Transactional
	public void addByFavorites(Favorites favorites) throws MyException {
		YrElder elderEntity = favorites.getFavorite(YrElder.class);
		YrElderFamilyMember elderFamilyEntity = favorites.getFavorite(YrElderFamilyMember.class);
		YrElderMemContact elderMemContactEntity = favorites.getFavorite(YrElderMemContact.class);
		
		this.repository.save(elderEntity);
		familyMemberRepository.save(elderFamilyEntity);
		memContactRepository.save(elderMemContactEntity);
	}

	public void modifyByFavorites(Favorites entitys) throws MyException {
		// TODO Auto-generated method stub
		
	}

	@Transactional
	public void costSettlement(FavoriteSet favoriteSet) throws MyException {
		List<YrBeHospitalPledge> beHospitalPledges = favoriteSet.getFavorite("beHospitalPledges", List.class);
//		List<YrLeasePledge> leasePledges = favoriteSet.getFavorite("leasePledges", List.class);
		YrElderWallet elderWallet = favoriteSet.getFavorite("elderWallet", YrElderWallet.class);
		YrMealCard mealCard = favoriteSet.getFavorite("mealCard", YrMealCard.class);
		List<YrRoomAssetComp> roomAssetComps = favoriteSet.getFavorite("roomAssetComps", List.class);		
//		List<YrElderArrearage> elderArrearages = favoriteSet.getFavorite("elderArrearages",List.class);
		YrElderBeHospital elderBeHospital = favoriteSet.getFavorite("elderBeHospital", YrElderBeHospital.class);
//		YrBed bed = favoriteSet.getFavorite("bed", YrBed.class);
		List<YrElderOutOrder> elderOutOrders = favoriteSet.getFavorite("elderOutOrders", List.class);
		YrOutFeeRecord outFeeRecord = favoriteSet.getFavorite("outFeeRecord",YrOutFeeRecord.class);
		YrPaperRecord paperRecord = favoriteSet.getFavorite("paperRecord", YrPaperRecord.class);
		YrPaper paper = favoriteSet.getFavorite("paper", YrPaper.class);
		
		for(YrBeHospitalPledge entity : beHospitalPledges){
			beHospitalPledgeRepository.save(entity);
		}
//		for(YrLeasePledge entity : leasePledges){
//			leasePledgeRepository.save(entity);
//		}
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		if(null!=mealCard){
			mealCardRepositoty.save(mealCard);
		}
		for(YrRoomAssetComp entity : roomAssetComps){
			roomAssetCompRepository.save(entity);
		}
//		for(YrElderArrearage entity : elderArrearages){
//			elderArrearageRepository.save(entity);
//		}
		if(null!=elderBeHospital){
			elderBeHospitalRepository.save(elderBeHospital);
		}
//		if(null!=bed){
//			bedRepository.save(bed);
//		}
		for(YrElderOutOrder elderOutOrder : elderOutOrders){
			elderOutOrderRepository.save(elderOutOrder);
		}
		if(outFeeRecord!=null){
			outFeeRecordRepository.save(outFeeRecord);
		}
		
		paperRecordRepository.save(paperRecord);
		paperRepository.save(paper);
	}

}
